Public Class ClassRunningNumber
    Dim sTrSql As String
    Dim cLsConnect As New Class_Connection
    Function SelectAutoNumber(ByVal TypeData As Integer, ByVal YearData As Integer, ByVal cOnnectionString As String) As String
        Dim cOnn As New OleDb.OleDbConnection
        Dim dS As New DataSet
        cOnn = cLsConnect.ConnectionOpen(cOnnectionString)

        If TypeData = 0 Then
            'ReserveId
            sTrSql = "Select ReserveId  From InvRunningNumber Where (YearReserveId = " & YearData & ")"
            dS = cLsConnect.BindData(cOnn, , , sTrSql)
            If dS Is Nothing Then
                sTrSql = " Insert Into InvRunningNumber(YearReserveId,ReserveId,YearBdId,BdId) Values('" & YearData & "',1,'" & YearData & "',0)"
                cLsConnect.ExecSql(cOnn, sTrSql)
                Return "00001"
            Else
                sTrSql = " Update  InvRunningNumber Set ReserveId = ReserveId+1 Where (YearReserveId = " & YearData & ")"
                cLsConnect.ExecSql(cOnn, sTrSql)
                Return Format(CDec(dS.Tables(0).Rows(0).Item("ReserveId")) + 1, "00000")
            End If
        Else
            'Bdid
            sTrSql = "Select BdId  From InvRunningNumber Where (YearBdId = " & YearData & ")"
            dS = cLsConnect.BindData(cOnn, , , sTrSql)
            If dS Is Nothing Then
                sTrSql = " Insert Into InvRunningNumber(YearReserveId,ReserveId,YearBdId,BdId) Values('" & YearData & "',0,'" & YearData & "',1)"
                cLsConnect.ExecSql(cOnn, sTrSql)
                Return "00001"
            Else
                sTrSql = " Update  InvRunningNumber Set BdId = BdId+1 Where (YearBdId = " & YearData & ")"
                cLsConnect.ExecSql(cOnn, sTrSql)
                Return Format(CDec(dS.Tables(0).Rows(0).Item("BdId")) + 1, "00000")
            End If
        End If

        cLsConnect.ConnectionClose(cOnn)

    End Function

End Class
